package com.dengc.controller;

import com.dengc.mapper.GoodsSupplierMapper;
import com.dengc.pojo.GoodsSupplier;
import com.dengc.pojo.SysRole;
import com.dengc.pojo.SysUser;
import com.dengc.service.GoodsSupplierService;
import com.dengc.utils.Contants;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class GoodsSupplierController {

    @Autowired
    GoodsSupplierService goodsSupplierService;

    //动态查询分页展示
    @RequestMapping("/sys/goodssupplier/list")
    public String getAllgoodssupplierList(
            @RequestParam(value = "supplierName",required = false) String supplierName,
            @RequestParam(value = "supplierCode",required = false) String supplierCode,
            @RequestParam(value = "pageIndex",required = false) Integer pageIndex,
            HttpServletRequest request){
        //用户
        GoodsSupplier goodsSupplierCondition = new GoodsSupplier();
        if(supplierName!=null && !"".equals(supplierName)){
            goodsSupplierCondition.setSupplierName(supplierName);
            //回显模糊查询数据
            request.setAttribute("supplierName",supplierName);
        }
        if (supplierCode!=null && !"".equals(supplierCode)){
            goodsSupplierCondition.setSupplierCode(supplierCode);
            //回显roleid
            request.setAttribute("supplierCode",supplierCode);
        }
        int pageNum = 1;//默认设置为第一页
        if (pageIndex!=null){
            pageNum = pageIndex;
        }
        PageInfo<GoodsSupplier> pageInfo = goodsSupplierService.getGoodsSupplierByPage(goodsSupplierCondition, pageNum);
        request.setAttribute("pageInfo",pageInfo);
        return "goodsSupplier/list";
    }

    //供货商信息管理界面添加供货商信息功能 1.进入添加页面
    @RequestMapping("/addgoodsSupplier")
    public  String addgoodsSupplier1(){
        return "goodsSupplier/add";
    }

    //供货商信息管理界面添加供货商信息功能 2.添加供货商
    @RequestMapping("/sys/goodsSupplier/add")
    @ResponseBody
    public String addgoodsSupplier2(GoodsSupplier goodsSupplier, HttpServletRequest request, HttpSession session,
                                    @RequestParam(value = "attachs",required = false) MultipartFile photoImage,
                                    @RequestParam(value = "attachs1",required = false) MultipartFile photoImage1){
        SysUser user = (SysUser) session.getAttribute(Contants.USERSESSION);
        ServletContext context = session.getServletContext();
        String path = context.getRealPath("/statics/uploadFile/");
        File file = new  File(path);
        if (!file.exists()){
            //如果没有自动创建文件夹
            file.mkdirs();
        }
        try {
            String destFileName= uploadFile(photoImage,path,request);
            goodsSupplier.setCompanyLicPicPath(destFileName);
        } catch (IOException e) {
            request.setAttribute("msg","上传失败");
            throw new RuntimeException(e);
        }
        try {
            String destFileName= uploadFile(photoImage1,path,request);
            goodsSupplier.setOrgCodePicPath(destFileName);
        } catch (IOException e) {
            request.setAttribute("msg","上传失败");
            throw new RuntimeException(e);
        }


        goodsSupplier.setCreatedUserId(user.getId());
        goodsSupplier.setCreateTime(new Date());
        int num =  goodsSupplierService.addSupplier(goodsSupplier);
        if(num>0){
            //保存成功
            return "<script>alert('新增成功');location.href='"+request.getContextPath()+"/sys/goodssupplier/list'</script>";
        }else{
            //保存失败
            return "<script>alert('新增失败');location.href='"+request.getContextPath()+"/sys/goodssupplier/list'</script>";
        }
    }

    public String uploadFile(MultipartFile file,String path,HttpServletRequest request) throws IOException{
        String destFileName =null;
        if(file!=null) {
            //上传用户照片信息
            String fileName = file.getOriginalFilename();//文件名
            String fileNameSuffix = FilenameUtils.getExtension(fileName);
            //判断文件大小
            if (file.getSize() > 1024 * 1024 * 5) {
                return "<script>alert('上传文件过大！');location.href='" + request.getContextPath() + "/addgoodsSupplier'</script>";
            }
            if (!fileNameSuffix.equalsIgnoreCase("jpg") &&
                    !fileNameSuffix.equalsIgnoreCase("png") &&
                    !fileNameSuffix.equalsIgnoreCase("pneg") &&
                    !fileNameSuffix.equalsIgnoreCase("bmp")) {
                return "<script>alert('上传文件类型不正确！');location.href='" + request.getContextPath() + "//addgoodsSupplier'</script>";
            }
            //新文件名(生产唯一的文件名)
            destFileName = "images-" + UUID.randomUUID().toString() + "." + fileNameSuffix;
            File desFile = new File(path, destFileName);
            file.transferTo(desFile);
        }
        return destFileName;
    }

    //供货商信息管理界面查看供货商信息
    @RequestMapping("/getAllGoods/{id}")
    public String getAllGoods(@PathVariable("id") Integer id,HttpServletRequest request){
        GoodsSupplier goodsSupplier = goodsSupplierService.getAllGoodsById(id);
        request.setAttribute("goods",goodsSupplier);
        return "goodsSupplier/view";
    }

    //供货商信息管理界面修改供货商信息功能 1.进入修改页面
    @RequestMapping("/updateGoods/{id}")
    public String updateGoods1(@PathVariable("id") Integer id, HttpServletRequest request){
        GoodsSupplier goodsSupplier = goodsSupplierService.getAllGoodsById(id);
        request.setAttribute("goods",goodsSupplier);
        return "goodsSupplier/update";
    }

    //供货商信息管理界面修改供货商信息功能 2.进行供货商修改
    @RequestMapping("/sys/goodsSupplier/update")
    @ResponseBody
    public String updateGoods2(GoodsSupplier goodsSupplier,HttpServletRequest request){
        int num =  goodsSupplierService.updateSupplier(goodsSupplier);
        if(num>0){
            //保存成功
            return "<script>alert('修改成功');location.href='"+request.getContextPath()+"/sys/goodssupplier/list'</script>";
        }else{
            //保存失败
            return "<script>alert('修改失败');location.href='"+request.getContextPath()+"/sys/goodssupplier/list'</script>";
        }
    }

    //供货商管理界面删除供货商信息
    @RequestMapping("/deletGoods/{id}")
    @ResponseBody
    public String deletGoods(@PathVariable("id") Integer id,HttpServletRequest request){
        int num = goodsSupplierService.deletSupplier(id);
        if(num>0){
            //删除成功
            return "<script>alert('删除成功');location.href='"+request.getContextPath()+"/sys/goodssupplier/list'</script>";
        }else{
            //删除失败
            return "<script>alert('删除失败');location.href='"+request.getContextPath()+"/sys/goodssupplier/list'</script>";
        }
    }

}
